/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.adaptor.method;

//import Inspur.Gsp.Caf.Generator.BaseInfoItem.*;
//import Inspur.Gsp.Caf.Generator.ClassMethodGenerator.*;
//import Inspur.Gsp.Caf.Generator.Utils.*;
//import Inspur.Gsp.Cef.Repository.*;
//import Microsoft.CodeAnalysis.*;
//import Microsoft.CodeAnalysis.CSharp.*;
//import Microsoft.CodeAnalysis.CSharp.Syntax.*;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.repository.IRootRepository;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.repository.adaptor.EntityRelationalAdaptor;
import com.inspur.edp.cef.repository.assembler.AssociationInfo;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetAssosPropDBMappingGenerator extends ClassMethodGenerator {
	private static final String ColumnsVariableName = "refColumns%1$s";
	private static final String RepVariableName = "repository%1$s";
	private ArrayList<IGspCommonField> commonFields;
//	private Class entityReposFactory;

	public GetAssosPropDBMappingGenerator(ArrayList<IGspCommonField> commonFields) {
		this.commonFields = commonFields;
//		this.entityReposFactory = entityReposFactory;
	}

	//#region Impl

	@Override
	protected boolean getIsOverride() {
		return false;
	}

	@Override
	protected String getMethodName() {
		return "getAssosPropDBMapping";
	}

	@Override
	protected TypeInfo getReturnType() {
		TypeInfo typeInfo=new TypeInfo(HashMap.class.getTypeName());
		typeInfo.setIsParameterizedType(true);
		typeInfo.getArguments().add(new TypeInfo(String.class.getTypeName()));
		typeInfo.getArguments().add(new TypeInfo(String.class.getTypeName()));
		return typeInfo;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list = new ArrayList<Modifier.ModifierKeyword>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		ArrayList<ParameterInfo> list =new ArrayList<ParameterInfo>();
		ParameterInfo parameterInfo=new ParameterInfo();
		parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
		parameterInfo.setParamName("propName");
		list.add(parameterInfo);

		return list;
	}

	@Override
	protected Block buildMethodBody() {
		Block block = ast.newBlock();
		this.addImport(EntityRelationalAdaptor.class.getTypeName());
		SwitchStatement switchStatement = ast.newSwitchStatement();
		switchStatement.setExpression(ast.newSimpleName("propName"));
		for (IGspCommonField field : commonFields) {
			SwitchCase switchCase = ast.newSwitchCase();
			StringLiteral stringLiteral = ast.newStringLiteral();
			stringLiteral.setLiteralValue(field.getLabelID());
			switchCase.setExpression(stringLiteral);

			MethodInvocation methodInvocation = ast.newMethodInvocation();
			methodInvocation.setName(ast.newSimpleName("get"+ field.getLabelID() +"AssoDBMapping"));
			ReturnStatement returnStatement = ast.newReturnStatement();
			returnStatement.setExpression(methodInvocation);

			switchStatement.statements().add(switchCase);
			switchStatement.statements().add(returnStatement);
		}
		//default
		SwitchCase defaultSwitchCase = ast.newSwitchCase();
		defaultSwitchCase.setExpression(null);

		ParameterizedType parameterizedType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("HashMap")));
		parameterizedType.typeArguments().add(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(String.class).getType(ast));
		parameterizedType.typeArguments().add(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(String.class).getType(ast));
		ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
		classInstanceCreation.setType(parameterizedType);

		ReturnStatement returnStatement = ast.newReturnStatement();
		returnStatement.setExpression(classInstanceCreation);

	    switchStatement.statements().add(defaultSwitchCase);
	    switchStatement.statements().add(returnStatement);

		block.statements().add(switchStatement);
		return block;
	}
}
